package com.niiwoo.civet.account.service.local.openAccount2.listener;

import com.alibaba.fastjson.JSON;
import com.niiwoo.asset.common.utils.StringUtils;
import com.niiwoo.asset.org.business.constants.OrgRabbitConstant;
import com.niiwoo.asset.org.business.dto.response.OpenAccountCallbackRespDTO;
import com.niiwoo.civet.account.dao.entity.AccountBase;
import com.niiwoo.civet.account.dao.entity.BankCard;
import com.niiwoo.civet.account.dao.entity.OpenAccountRecord;
import com.niiwoo.civet.account.dao.mapper.BankCardMapperExt;
import com.niiwoo.civet.account.dao.mapper.BankTypeMapperExt;
import com.niiwoo.civet.account.enums.OpenAccountRecordStatusEnum;
import com.niiwoo.civet.account.enums.OpenAccountWayEnum;
import com.niiwoo.civet.account.service.local.AccountService;
import com.niiwoo.civet.account.service.local.openAccount2.dto.OpenAccountEvent;
import com.niiwoo.tripod.base.component.PrivacyMasks;
import com.niiwoo.tripod.lanmao.enums.UserRoleEnum;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;

/**
 * 机构极速借个人绑卡注册监听器
 * @author TomXue
 * @date 2018-04-29
 */
@Slf4j
@Component
public class OrgOpenAccountListener implements OpenAccountListener {

    private static final List<OpenAccountWayEnum> INTERESTED_WAY_LIST = Arrays.asList(OpenAccountWayEnum.ORG_BIND_BANK_CARD_REGISTER);

    private static final List<OpenAccountRecordStatusEnum> EXCEPT_STATUS = Arrays.asList(OpenAccountRecordStatusEnum.SUCCESS,OpenAccountRecordStatusEnum.FAILURE);

    @Autowired
    BankCardMapperExt bankCardMapperExt;

    @Autowired
    BankTypeMapperExt bankTypeMapperExt;

    @Autowired
    AccountService accountService;

    @Autowired
    PrivacyMasks privacyMasks;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Transactional(isolation = Isolation.READ_COMMITTED)
    @Override
    public void handle(OpenAccountEvent event) {
        OpenAccountRecord record = event.getRecord();
        boolean isBorrowAccount = UserRoleEnum.enumOf(record.getAccountRole()) == UserRoleEnum.BORROWERS;
        if (!isBorrowAccount) {
            log.error("机构极速借个人开户回调,用户ID={},机构ID={},账户角色异常:{}",record.getUserId(),record.getOrgId(),record.getAccountRole());
            return;
        }

        // 查询机构账户
        AccountBase accountBase = accountService.getAccount(record.getUserId(),UserRoleEnum.enumOf(record.getAccountRole()),record.getOrgId());
        if(accountBase == null){
            log.error("机构极速借个人开户回调,用户ID={},机构ID={},查询账户信息异常:{}",record.getUserId(),record.getOrgId(),JSON.toJSONString(record));
            return;
        }

        BankCard bankCard = bankCardMapperExt.selectOrgUserBankCard(accountBase.getId());
        OpenAccountCallbackRespDTO openAccountCallbackRespDTO=new OpenAccountCallbackRespDTO();
        openAccountCallbackRespDTO.setUserId(record.getUserId());
        openAccountCallbackRespDTO.setBankCardNo(StringUtils.isEmpty(bankCard.getBankAccountNo())?null:privacyMasks.decryptPrivacy(bankCard.getBankAccountNo()));
        openAccountCallbackRespDTO.setBankCode(bankCard.getBankCode());
        openAccountCallbackRespDTO.setBankMobile(StringUtils.isEmpty(bankCard.getMobileNo())?null:privacyMasks.decryptPrivacy(bankCard.getMobileNo()));
        openAccountCallbackRespDTO.setOpenAccountRecordStatus(record.getStatus());
        openAccountCallbackRespDTO.setOrgId(record.getOrgId());

        log.info("机构极速借个人开户回调,用户ID={},机构ID={},通知参数={}", record.getUserId(),record.getOrgId(),JSON.toJSONString(openAccountCallbackRespDTO));
        String exchange = OrgRabbitConstant.Exchange.ORG_COMMON_DIRECT_EXCHANGE;
        rabbitTemplate.convertAndSend(exchange, OrgRabbitConstant.getRoutingKey(exchange,OrgRabbitConstant.Queue.OPEN_ACCOUNT_CALLBACK_QUEUE), openAccountCallbackRespDTO);
    }

    @Override
    public boolean isExpectStatus(OpenAccountRecordStatusEnum status) {
        return EXCEPT_STATUS.contains(status);
    }

    @Override
    public boolean isInterestedOpenAccountWay(OpenAccountWayEnum way) {
        return INTERESTED_WAY_LIST.contains(way);
    }


    @Getter
    @Setter
    private static class OrgMQ implements Serializable {

        private static final long serialVersionUID = 2086740069815393982L;

        private String userId;

        private String bankCardNo;

        private String bankCode;

        private Byte openAccountRecordStatus;

        private String orgId;
    }

}
